package com.cater.oa.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.oa.entity.OaMeeting;
import com.cater.oa.entity.OaMeetingSure;
import com.cater.oa.service.OaMeetingServiceImpl;
import com.cater.oa.service.OaMeetingSureServiceImpl;
import com.cater.query.CommonQuery;

/**
 * 描述：会议室预订控制层
 * 
 * @author yong
 * @date 2019/04/08
 */
@Controller
@RequestMapping("/meeting")
public class OaMeetingController {

	@Autowired
	private OaMeetingServiceImpl service;
	@Autowired
	private OaMeetingSureServiceImpl sureService;

	/**
	 * @会议室预订管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "oa/meeting/list";
	}

	/**
	 * @会议室预订管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询会议室预订分页数据")
	public PageResult<OaMeeting> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<OaMeeting> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("meeting:add")
	public String addView() {
		return "oa/meeting/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("meeting:add")
	@ResponseBody
	@SystemControllerLog(description = "添加会议室预订")
	public JSONObject add(OaMeeting t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			String id = service.saves(t);
			String conferees = t.getConferees();
			if (StringUtils.isNotBlank(id)) {
				if (StringUtils.isNotEmpty(conferees)) {
					String[] ids = conferees.split(",");
					for (int i = 0; i < ids.length; i++) {
						OaMeetingSure sure = new OaMeetingSure();
						sure.setMeetingId(id);
						sure.setConferee(ids[i]);
						sure.setAttendee(0);
						sureService.saves(sure);
					}
				}
				json.put("code", 200);
				json.put("message", "success");
			} else {
				json.put("code", 400);
				json.put("message", "fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("meeting:edit")
	public String editView(String id, Model model, CommonQuery query) {
		OaMeeting t = service.getById(id);
		model.addAttribute("meeting", t);
		return "oa/meeting/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("meeting:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改会议室预订")
	public JSONObject update(OaMeeting t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("meeting:delete")
	@SystemControllerLog(description = "删除会议室预订")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("meeting:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		OaMeeting t = service.getById(id);
		List<OaMeetingSure> sure = sureService.getByMeetingId(id);
		String userNames = "";
		for (OaMeetingSure oaMeetingSure : sure) {
			String user_name = oaMeetingSure.getConfereesName();
			if (StringUtils.isNotEmpty(user_name)) {
				userNames += user_name + " ";
			}
		}
		if (StringUtils.isEmpty(userNames)) {
			userNames = "暂无";
		}
		t.setConfereesName(userNames);
		model.addAttribute("meeting", t);
		return "oa/meeting/detail";
	}

}